package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.GoodsTypeVo;
import com.atguigu.jxc.service.GoodsTypeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/goodsType")
@RestController
public class GoodsTypeController {

    @Autowired
    GoodsTypeService goodsTypeService;

    @PostMapping("/loadGoodsType")
    public String  loadGoodsType() {
        List<GoodsTypeVo> list = goodsTypeService.loadGoodsType();
        String json = null;
        try {
            json = new Gson().toJson(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping("/save")
    public ServiceVO save(String  goodsTypeName,Integer  pId){
        goodsTypeService.save(goodsTypeName,pId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
    @PostMapping("/delete")
    public ServiceVO delete(Integer  goodsTypeId){
        goodsTypeService.delete(goodsTypeId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

}
